﻿using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace Microsoft.Office.PowerPoint.STAMP.Core
{
    public static class UtilXmlToString
    {
        /// <summary>
        /// Reconstruct an object from an XML string
        /// </summary>
        /// <param name="xml">XML string</param>
        /// <returns>Deserialized object of T</returns>
        public static T DeserializeObject<T>(string xml)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof (T));
            return (T) xmlSerializer.Deserialize(new StringReader(xml));
        }

        /// <summary>
        /// Serialize an object into an XML string
        /// </summary>
        /// <typeparam name="T">Generic object</typeparam>
        /// <param name="obj">Object to serialize</param>
        /// <returns>XML string value of object</returns>
        public static string SerializeObject<T>(T obj)
        {
            var output = new StringWriter();
            var writer = XmlWriter.Create(output);
            new XmlSerializer(typeof (T)).Serialize(writer, obj);
            return output.ToString();
        }
    }
}